<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends CI_Controller {
    public  $page_name ="article";
    private     $validate_config = array(
            array(
                'field' => 'title',
                'label' => '标题',
                'rules' => 'required'
            ),
            array(
                'field' => 'catalog_id',
                'label' => '分类',
                'rules' => 'required'
            ),
            array(
                'field' => 'img',
                'label' => '图片',
                'rules' => 'required'
            ),
            array(
                'field' => 'desc',
                'label' => '描述',
                'rules' => 'required'
            ),
            array(
                'field' => 'content',
                'label' => '内容',
                'rules' => 'required'
            ),
          
        );
	 public function __construct(){
        parent::__construct();
    if(!$this->session->userdata('is_login')){
         redirect("/admin/auth");
 }
}

    public function index($page=1){
    $this->load->model('Article_model');
    $total = $this->Article_model->count_all();
    $page_size=5;
    $articles=$this->Article_model->get_by_page($page,$page_size);
    $data = array(
       'articles'=>$articles
       
        );

    $this->load->library('pagination');
    $config['base_url'] = site_url('/admin/article/index/');
    $config['total_rows'] = $total;
    $config['per_page'] = $page_size;
    $this->pagination->initialize($config);
    $this->load->view('admin/Article',$data);
}
 
 public function add()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules($this->validate_config);
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->load->model('Catalog_model');


        if ($this->form_validation->run() == FALSE)
        {
            $catalogs=$this->Catalog_model->get_all();
           $article = new stdClass();
             $article->title = "";
             $article->catalog_id = 0;
             $article->img = "";
             $article->desc = "";
             $article->content = "";
           $data = array(
               'catalogs'=>$catalogs,
               'title'=>"添加",
               "article"=>$article
                );
            $this->load->view('admin/Article_form', $data);
        }
        else
        {
        $data=$this->input->post();
        $this->load->model("Article_model");
        $this->Article_model->add_one($data);
        redirect("admin/article/index");

            
        }
    }
    
     public function edit($id)
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
           $this->form_validation->set_rules($this->validate_config);
        $this->load->model('Catalog_model');
         $this->load->model('Article_model');
         $catalogs=$this->Catalog_model->get_all();
        
        if ($this->form_validation->run() == FALSE)
        {
             $data = array(
               'catalogs'=>$this->Catalog_model-> get_all(),
               'title'=>"编辑",
             
               'article'=>$this->Article_model->get_one($id),
              
            );
           
            $this->load->view('admin/Article_form', $data);
        }
        else
        {
        $data=$this->input->post();
        $this->load->model("Article_model");
        $this->Article_model->edit_one($id,$data);
        redirect("admin/article/index");
       }
    }
    public function delete($id){
        $this->load->model("Article_model");
        $this->Article_model->delete($id);
        redirect("admin/article/index");
    }

    public function recommend($id){
      $this->load->model("Article_model"); 
       $this->Article_model->recommend($id);
         redirect("admin/article/index");
    }

}
